Code
a=1
type(a)int
Tony Duan
July 6, 2023

len()count()index()show the first ‘apple’ index. python list start at 0
all ‘apple’ in the list
['apple', 'banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
using loop:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
using List Comprehensions
('orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'apple')
tuple can not be modified.
A set is an unordered collection with no duplicate elements.
{'apple', 'banana', 'orange', 'pear'}
NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object
Python doesn’t have a built-in type for matrices. However, we can treat a list of a list as a matrix
[[1, 4, 5, 12], [-5, 8, 9, 0], [-6, 7, 11, 19]]
numpy Array
first row
first column
first row and first column element
2 row and 3 column
https://docs.python.org/3/tutorial/datastructures.html#
https://numpy.org/doc/stable/user/basics.rec.html
---
title: "data type and data structure in Python"
author: "Tony Duan"
date: "2023-07-06"
categories: [Python]
execute:
warning: false
error: false
format:
html:
code-fold: show
code-tools: true
number-sections: true
---
{width="500"}
# bulid-in data Structures
## singular
```{python}
a=1
type(a)
```
```{python}
a=1.3
type(a)
```
```{python}
a='hell'
type(a)
```
## list
```{python}
a=[1,2,3]
a
```
```{python}
type(a)
```
```{python}
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana','apple']
```
### find length of the list with `len()`
```{python}
len(fruits)
```
### find how many time in the list with `count()`
```{python}
fruits.count('apple')
```
### find locaiton of on the list with `index()`
show the first 'apple' index. python list start at 0
```{python}
fruits.index('apple')
```
all 'apple' in the list
```{python}
[index for index, value in enumerate(fruits) if value == 'apple']
```
## reverse the list
```{python}
fruits.reverse()
fruits
```
### sort the list
```{python}
fruits.sort()
fruits
```
### add element on the list
```{python}
fruits.append('grape')
fruits
```
## drop last element
```{python}
fruits.pop()
fruits
```
### List Comprehensions
using loop:
```{python}
squares = []
for x in range(10):
squares.append(x**2)
squares
```
using List Comprehensions
```{python}
squares = [x**2 for x in range(10)]
squares
```
## Tuples
```{python}
fruits = ('orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana','apple')
fruits
```
```{python}
type(fruits)
```
tuple can not be modified.
## Sets
A set is an unordered collection with no duplicate elements.
```{python}
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
basket
```
```{python}
type(basket)
```
## Dictionaries
```{python}
tel = {'jack': 4098, 'sape': 4139}
tel
```
```{python}
type(tel)
```
```{python}
tel['jack']
```
# numpy data Structures(matrix in python)
NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object
Python doesn't have a built-in type for matrices. However, we can treat a list of a list as a matrix
```{python}
A = [[1, 4, 5, 12],
[-5, 8, 9, 0],
[-6, 7, 11, 19]]
A
```
numpy Array
```{python}
import numpy as np
A = np.array([[1, 2, 3], [3, 4, 5]])
print(A)
```
```{python}
type(A)
```
### selection
first row
```{python}
A[0]
```
first column
```{python}
A[:,0]
```
first row and first column element
```{python}
A[0,0]
```
```{python}
A.dtype
```
2 row and 3 column
```{python}
A.shape
```
### reshape
```{python}
a=np.arange(9).reshape(3, 3)
a
```
# pandas data Structures(dataframe in python)
# Reference
https://docs.python.org/3/tutorial/datastructures.html#
https://numpy.org/doc/stable/user/basics.rec.html